home *** CD-ROM | disk | FTP | other *** search
/ PC Open 93 / PC Open 93 CD 1.bin / internet / AmphetaDesk / lib / Text / Template.pm
Encoding:
Perl POD Document  |  2002-03-25  |  58.7 KB  |  1,913 lines

  1. # -*- perl -*-
  2. # Text::Template.pm
  3. #
  4. # Fill in `templates'
  5. #
  6. # Copyright 1996, 1997, 1999, 2001, 2002 M-J. Dominus.
  7. # You may copy and distribute this program under the
  8. # same terms as Perl iteself.  
  9. # If in doubt, write to mjd-perl-template+@plover.com for a license.
  10. #
  11. # Version 1.43
  12.  
  13. package Text::Template;
  14. require 5.004;
  15. use Exporter;
  16. @ISA = qw(Exporter);
  17. @EXPORT_OK = qw(fill_in_file fill_in_string TTerror);
  18. use vars '$ERROR';
  19. use strict;
  20.  
  21. $Text::Template::VERSION = '1.43';
  22. my %GLOBAL_PREPEND = ('Text::Template' => '');
  23.  
  24. sub Version {
  25.   $Text::Template::VERSION;
  26. }
  27.  
  28. sub _param {
  29.   my $kk;
  30.   my ($k, %h) = @_;
  31.   for $kk ($k, "\u$k", "\U$k", "-$k", "-\u$k", "-\U$k") {
  32.     return $h{$kk} if exists $h{$kk};
  33.   }
  34.   return;
  35. }
  36.  
  37. sub always_prepend
  38. {
  39.   my $pack = shift;
  40.   my $old = $GLOBAL_PREPEND{$pack};
  41.   $GLOBAL_PREPEND{$pack} = shift;
  42.   $old;
  43. }
  44.  
  45. {
  46.   my %LEGAL_TYPE;
  47.   BEGIN { 
  48.     %LEGAL_TYPE = map {$_=>1} qw(FILE FILEHANDLE STRING ARRAY);
  49.   }
  50.   sub new {
  51.     my $pack = shift;
  52.     my %a = @_;
  53.     my $stype = uc(_param('type', %a)) || 'FILE';
  54.     my $source = _param('source', %a);
  55.     my $untaint = _param('untaint', %a);
  56.     my $prepend = _param('prepend', %a);
  57.     my $alt_delim = _param('delimiters', %a);
  58.     my $broken = _param('broken', %a);
  59.     unless (defined $source) {
  60.       require Carp;
  61.       Carp::croak("Usage: $ {pack}::new(TYPE => ..., SOURCE => ...)");
  62.     }
  63.     unless ($LEGAL_TYPE{$stype}) {
  64.       require Carp;
  65.       Carp::croak("Illegal value `$stype' for TYPE parameter");
  66.     }
  67.     my $self = {TYPE => $stype,
  68.         PREPEND => $prepend,
  69.                 UNTAINT => $untaint,
  70.                 BROKEN => $broken,
  71.         (defined $alt_delim ? (DELIM => $alt_delim) : ()),
  72.            };
  73.     # Under 5.005_03, if any of $stype, $prepend, $untaint, or $broken
  74.     # are tainted, all the others become tainted too as a result of
  75.     # sharing the expression with them.  We install $source separately
  76.     # to prevent it from acquiring a spurious taint.
  77.     $self->{SOURCE} = $source;
  78.  
  79.     bless $self => $pack;
  80.     return unless $self->_acquire_data;
  81.     
  82.     $self;
  83.   }
  84. }
  85.  
  86. # Convert template objects of various types to type STRING,
  87. # in which the template data is embedded in the object itself.
  88. sub _acquire_data {
  89.   my ($self) = @_;
  90.   my $type = $self->{TYPE};
  91.   if ($type eq 'STRING') {
  92.     # nothing necessary    
  93.   } elsif ($type eq 'FILE') {
  94.     my $data = _load_text($self->{SOURCE});
  95.     unless (defined $data) {
  96.       # _load_text already set $ERROR
  97.       return undef;
  98.     }
  99.     if ($self->{UNTAINT} && _is_clean($self->{SOURCE})) {
  100.       _unconditionally_untaint($data);
  101.     }
  102.     $self->{TYPE} = 'STRING';
  103.     $self->{FILENAME} = $self->{SOURCE};
  104.     $self->{SOURCE} = $data;
  105.   } elsif ($type eq 'ARRAY') {
  106.     $self->{TYPE} = 'STRING';
  107.     $self->{SOURCE} = join '', @{$self->{SOURCE}};
  108.   } elsif ($type eq 'FILEHANDLE') {
  109.     $self->{TYPE} = 'STRING';
  110.     local $/;
  111.     my $fh = $self->{SOURCE};
  112.     my $data = <$fh>; # Extra assignment avoids bug in Solaris perl5.00[45].
  113.     if ($self->{UNTAINT}) {
  114.       _unconditionally_untaint($data);
  115.     }
  116.     $self->{SOURCE} = $data;
  117.   } else {
  118.     # This should have been caught long ago, so it represents a 
  119.     # drastic `can't-happen' sort of failure
  120.     my $pack = ref $self;
  121.     die "Can only acquire data for $pack objects of subtype STRING, but this is $type; aborting";
  122.   }
  123.   $self->{DATA_ACQUIRED} = 1;
  124. }
  125.  
  126. sub source {
  127.   my ($self) = @_;
  128.   $self->_acquire_data unless $self->{DATA_ACQUIRED};
  129.   return $self->{SOURCE};
  130. }
  131.  
  132. sub set_source_data {
  133.   my ($self, $newdata) = @_;
  134.   $self->{SOURCE} = $newdata;
  135.   $self->{DATA_ACQUIRED} = 1;
  136.   $self->{TYPE} = 'STRING';
  137.   1;
  138. }
  139.  
  140. sub compile {
  141.   my $self = shift;
  142.  
  143.   return 1 if $self->{TYPE} eq 'PREPARSED';
  144.  
  145.   return undef unless $self->_acquire_data;
  146.   unless ($self->{TYPE} eq 'STRING') {
  147.     my $pack = ref $self;
  148.     # This should have been caught long ago, so it represents a 
  149.     # drastic `can't-happen' sort of failure
  150.     die "Can only compile $pack objects of subtype STRING, but this is $self->{TYPE}; aborting";
  151.   }
  152.  
  153.   my @tokens;
  154.   my $delim_pats = shift() || $self->{DELIM};
  155.  
  156.   
  157.  
  158.   my ($t_open, $t_close) = ('{', '}');
  159.   my $DELIM;            # Regex matches a delimiter if $delim_pats
  160.   if (defined $delim_pats) {
  161.     ($t_open, $t_close) = @$delim_pats;
  162.     $DELIM = "(?:(?:\Q$t_open\E)|(?:\Q$t_close\E))";
  163.     @tokens = split /($DELIM|\n)/, $self->{SOURCE};
  164.   } else {
  165.     @tokens = split /(\\\\(?=\\*[{}])|\\[{}]|[{}\n])/, $self->{SOURCE};
  166.   }
  167.   my $state = 'TEXT';
  168.   my $depth = 0;
  169.   my $lineno = 1;
  170.   my @content;
  171.   my $cur_item = '';
  172.   my $prog_start;
  173.   while (@tokens) {
  174.     my $t = shift @tokens;
  175.     next if $t eq '';
  176.     if ($t eq $t_open) {    # Brace or other opening delimiter
  177.       if ($depth == 0) {
  178.     push @content, [$state, $cur_item, $lineno] if $cur_item ne '';
  179.     $cur_item = '';
  180.     $state = 'PROG';
  181.     $prog_start = $lineno;
  182.       } else {
  183.     $cur_item .= $t;
  184.       }
  185.       $depth++;
  186.     } elsif ($t eq $t_close) {    # Brace or other closing delimiter
  187.       $depth--;
  188.       if ($depth < 0) {
  189.     $ERROR = "Unmatched close brace at line $lineno";
  190.     return undef;
  191.       } elsif ($depth == 0) {
  192.     push @content, [$state, $cur_item, $prog_start] if $cur_item ne '';
  193.     $state = 'TEXT';
  194.     $cur_item = '';
  195.       } else {
  196.     $cur_item .= $t;
  197.       }
  198.     } elsif (!$delim_pats && $t eq '\\\\') { # precedes \\\..\\\{ or \\\..\\\}
  199.       $cur_item .= '\\';
  200.     } elsif (!$delim_pats && $t =~ /^\\([{}])$/) { # Escaped (literal) brace?
  201.     $cur_item .= $1;
  202.     } elsif ($t eq "\n") {    # Newline
  203.       $lineno++;
  204.       $cur_item .= $t;
  205.     } else {            # Anything else
  206.       $cur_item .= $t;
  207.     }
  208.   }
  209.  
  210.   if ($state eq 'PROG') {
  211.     $ERROR = "End of data inside program text that began at line $prog_start";
  212.     return undef;
  213.   } elsif ($state eq 'TEXT') {
  214.     push @content, [$state, $cur_item, $lineno] if $cur_item ne '';
  215.   } else {
  216.     die "Can't happen error #1";
  217.   }
  218.   
  219.   $self->{TYPE} = 'PREPARSED';
  220.   $self->{SOURCE} = \@content;
  221.   1;
  222. }
  223.  
  224. sub prepend_text {
  225.   my ($self) = @_;
  226.   my $t = $self->{PREPEND};
  227.   unless (defined $t) {
  228.     $t = $GLOBAL_PREPEND{ref $self};
  229.     unless (defined $t) {
  230.       $t = $GLOBAL_PREPEND{'Text::Template'};
  231.     }
  232.   }
  233.   $self->{PREPEND} = $_[1] if $#_ >= 1;
  234.   return $t;
  235. }
  236.  
  237. sub fill_in {
  238.   my $fi_self = shift;
  239.   my %fi_a = @_;
  240.  
  241.   unless ($fi_self->{TYPE} eq 'PREPARSED') {
  242.     my $delims = _param('delimiters', %fi_a);
  243.     my @delim_arg = (defined $delims ? ($delims) : ());
  244.     $fi_self->compile(@delim_arg)
  245.       or return undef;
  246.   }
  247.  
  248.   my $fi_varhash = _param('hash', %fi_a);
  249.   my $fi_package = _param('package', %fi_a) ;
  250.   my $fi_broken  = 
  251.     _param('broken', %fi_a)  || $fi_self->{BROKEN} || \&_default_broken;
  252.   my $fi_broken_arg = _param('broken_arg', %fi_a) || [];
  253.   my $fi_safe = _param('safe', %fi_a);
  254.   my $fi_ofh = _param('output', %fi_a);
  255.   my $fi_eval_package;
  256.   my $fi_scrub_package = 0;
  257.   my $fi_filename = _param('filename') || $fi_self->{FILENAME} || 'template';
  258.  
  259.   my $fi_prepend = _param('prepend', %fi_a);
  260.   unless (defined $fi_prepend) {
  261.     $fi_prepend = $fi_self->prepend_text;
  262.   }
  263.  
  264.   if (defined $fi_safe) {
  265.     $fi_eval_package = 'main';
  266.   } elsif (defined $fi_package) {
  267.     $fi_eval_package = $fi_package;
  268.   } elsif (defined $fi_varhash) {
  269.     $fi_eval_package = _gensym();
  270.     $fi_scrub_package = 1;
  271.   } else {
  272.     $fi_eval_package = caller;
  273.   }
  274.  
  275.   my $fi_install_package;
  276.   if (defined $fi_varhash) {
  277.     if (defined $fi_package) {
  278.       $fi_install_package = $fi_package;
  279.     } elsif (defined $fi_safe) {
  280.       $fi_install_package = $fi_safe->root;
  281.     } else {
  282.       $fi_install_package = $fi_eval_package; # The gensymmed one
  283.     }
  284.     _install_hash($fi_varhash => $fi_install_package);
  285.   }
  286.  
  287.   if (defined $fi_package && defined $fi_safe) {
  288.     no strict 'refs';
  289.     # Big fat magic here: Fix it so that the user-specified package
  290.     # is the default one available in the safe compartment.
  291.     *{$fi_safe->root . '::'} = \%{$fi_package . '::'};   # LOD
  292.   }
  293.  
  294.   my $fi_r = '';
  295.   my $fi_item;
  296.   foreach $fi_item (@{$fi_self->{SOURCE}}) {
  297.     my ($fi_type, $fi_text, $fi_lineno) = @$fi_item;
  298.     if ($fi_type eq 'TEXT') {
  299.       if ($fi_ofh) {
  300.     print $fi_ofh $fi_text;
  301.       } else {
  302.     $fi_r .= $fi_text;
  303.       }
  304.     } elsif ($fi_type eq 'PROG') {
  305.       no strict;
  306.       my $fi_lcomment = "#line $fi_lineno $fi_filename";
  307.       my $fi_progtext = 
  308.         "package $fi_eval_package; $fi_prepend;\n$fi_lcomment\n$fi_text;";
  309.       my $fi_res;
  310.       my $fi_eval_err = '';
  311.       if ($fi_safe) {
  312.         $fi_safe->reval(q{undef $OUT});
  313.     $fi_res = $fi_safe->reval($fi_progtext);
  314.     $fi_eval_err = $@;
  315.     my $OUT = $fi_safe->reval('$OUT');
  316.     $fi_res = $OUT if defined $OUT;
  317.       } else {
  318.     my $OUT;
  319.     $fi_res = eval $fi_progtext;
  320.     $fi_eval_err = $@;
  321.     $fi_res = $OUT if defined $OUT;
  322.       }
  323.  
  324.       # If the value of the filled-in text really was undef,
  325.       # change it to an explicit empty string to avoid undefined
  326.       # value warnings later.
  327.       $fi_res = '' unless defined $fi_res;
  328.  
  329.       if ($fi_eval_err) {
  330.     $fi_res = $fi_broken->(text => $fi_text,
  331.                    error => $fi_eval_err,
  332.                    lineno => $fi_lineno,
  333.                    arg => $fi_broken_arg,
  334.                    );
  335.     if (defined $fi_res) {
  336.       if (defined $fi_ofh) {
  337.         print $fi_ofh $fi_res;
  338.       } else {
  339.         $fi_r .= $fi_res;
  340.       }
  341.     } else {
  342.       return $fi_res;        # Undefined means abort processing
  343.     }
  344.       } else {
  345.     if (defined $fi_ofh) {
  346.       print $fi_ofh $fi_res;
  347.     } else {
  348.       $fi_r .= $fi_res;
  349.     }
  350.       }
  351.     } else {
  352.       die "Can't happen error #2";
  353.     }
  354.   }
  355.  
  356.   _scrubpkg($fi_eval_package) if $fi_scrub_package;
  357.   defined $fi_ofh ? 1 : $fi_r;
  358. }
  359.  
  360. sub fill_this_in {
  361.   my $pack = shift;
  362.   my $text = shift;
  363.   my $templ = $pack->new(TYPE => 'STRING', SOURCE => $text, @_)
  364.     or return undef;
  365.   $templ->compile or return undef;
  366.   my $result = $templ->fill_in(@_);
  367.   $result;
  368. }
  369.  
  370. sub fill_in_string {
  371.   my $string = shift;
  372.   my $package = _param('package', @_);
  373.   push @_, 'package' => scalar(caller) unless defined $package;
  374.   Text::Template->fill_this_in($string, @_);
  375. }
  376.  
  377. sub fill_in_file {
  378.   my $fn = shift;
  379.   my $templ = Text::Template->new(TYPE => 'FILE', SOURCE => $fn, @_)
  380.     or return undef;
  381.   $templ->compile or return undef;
  382.   my $text = $templ->fill_in(@_);
  383.   $text;
  384. }
  385.  
  386. sub _default_broken {
  387.   my %a = @_;
  388.   my $prog_text = $a{text};
  389.   my $err = $a{error};
  390.   my $lineno = $a{lineno};
  391.   chomp $err;
  392. #  $err =~ s/\s+at .*//s;
  393.   "Program fragment delivered error ``$err''";
  394. }
  395.  
  396. sub _load_text {
  397.   my $fn = shift;
  398.   local *F;
  399.   unless (open F, $fn) {
  400.     $ERROR = "Couldn't open file $fn: $!";
  401.     return undef;
  402.   }
  403.   local $/;
  404.   <F>;
  405. }
  406.  
  407. sub _is_clean {
  408.   my $z;
  409.   eval { ($z = join('', @_)), eval '#' . substr($z,0,0); 1 }   # LOD
  410. }
  411.  
  412. sub _unconditionally_untaint {
  413.   for (@_) {
  414.     ($_) = /(.*)/s;
  415.   }
  416. }
  417.  
  418. {
  419.   my $seqno = 0;
  420.   sub _gensym {
  421.     __PACKAGE__ . '::GEN' . $seqno++;
  422.   }
  423.   sub _scrubpkg {
  424.     my $s = shift;
  425.     no strict 'refs';
  426.     my $hash = $Text::Template::{$s."::"};
  427.     foreach my $key (%$hash) {
  428.       delete $hash->{$key};
  429.     }
  430.     delete $Text::Template::{$s."::"};
  431.   }
  432. }
  433.   
  434. # Given a hashful of variables (or a list of such hashes)
  435. # install the variables into the specified package,
  436. # overwriting whatever variables were there before.
  437. sub _install_hash {
  438.   my $hashlist = shift;
  439.   my $dest = shift;
  440.   if (UNIVERSAL::isa($hashlist, 'HASH')) {
  441.     $hashlist = [$hashlist];
  442.   }
  443.   my $hash;
  444.   foreach $hash (@$hashlist) {
  445.     my $name;
  446.     foreach $name (keys %$hash) {
  447.       my $val = $hash->{$name};
  448.       no strict 'refs';
  449.       local *SYM = *{"$ {dest}::$name"};
  450.       if (! defined $val) {
  451.     delete ${"$ {dest}::"}{$name};
  452.       } elsif (ref $val) {
  453.     *SYM = $val;
  454.       } else {
  455.      *SYM = \$val;
  456.       }
  457.     }
  458.   }
  459. }
  460.  
  461. sub TTerror { $ERROR }
  462.  
  463. 1;
  464.  
  465.  
  466. =head1 NAME 
  467.  
  468. Text::Template - Expand template text with embedded Perl
  469.  
  470. =head1 VERSION
  471.  
  472. This file documents C<Text::Template> version B<1.43>
  473.  
  474. =head1 SYNOPSIS
  475.  
  476.  use Text::Template;
  477.  
  478.  
  479.  $template = Text::Template->new(TYPE => FILE,  SOURCE => 'filename.tmpl');
  480.  $template = Text::Template->new(TYPE => ARRAY, SOURCE => [ ... ] );
  481.  $template = Text::Template->new(TYPE => FILEHANDLE, SOURCE => $fh );
  482.  $template = Text::Template->new(TYPE => STRING, SOURCE => '...' );
  483.  $template = Text::Template->new(PREPEND => q{use strict;}, ...);
  484.  
  485.  # Use a different template file syntax:
  486.  $template = Text::Template->new(DELIMITERS => [$open, $close], ...);
  487.  
  488.  $recipient = 'King';
  489.  $text = $template->fill_in();  # Replaces `{$recipient}' with `King'
  490.  print $text;
  491.  
  492.  $T::recipient = 'Josh';
  493.  $text = $template->fill_in(PACKAGE => T);
  494.  
  495.  # Pass many variables explicitly
  496.  $hash = { recipient => 'Abed-Nego',
  497.            friends => [ 'me', 'you' ],
  498.            enemies => { loathsome => 'Bill Gates',
  499.                         fearsome => 'Larry Ellison' },
  500.          };
  501.  $text = $template->fill_in(HASH => $hash, ...);
  502.  # $recipient is Abed-Nego,
  503.  # @friends is ( 'me', 'you' ),
  504.  # %enemies is ( loathsome => ..., fearsome => ... )
  505.  
  506.  
  507.  # Call &callback in case of programming errors in template
  508.  $text = $template->fill_in(BROKEN => \&callback, BROKEN_ARG => $ref, ...);
  509.  
  510.  # Evaluate program fragments in Safe compartment with restricted permissions
  511.  $text = $template->fill_in(SAFE => $compartment, ...);
  512.  
  513.  # Print result text instead of returning it
  514.  $success = $template->fill_in(OUTPUT => \*FILEHANDLE, ...);
  515.  
  516.  # Parse template with different template file syntax:
  517.  $text = $template->fill_in(DELIMITERS => [$open, $close], ...);
  518.  # Note that this is *faster* than using the default delimiters
  519.  
  520.  # Prepend specified perl code to each fragment before evaluating:
  521.  $text = $template->fill_in(PREPEND => q{use strict 'vars';}, ...);
  522.  
  523.  use Text::Template 'fill_in_string';
  524.  $text = fill_in_string( <<EOM, PACKAGE => 'T', ...);
  525.  Dear {$recipient},
  526.  Pay me at once.
  527.         Love, 
  528.          G.V.
  529.  EOM
  530.  
  531.  use Text::Template 'fill_in_file';
  532.  $text = fill_in_file($filename, ...);
  533.  
  534.  # All templates will always have `use strict vars' attached to all fragments
  535.  Text::Template->always_prepend(q{use strict 'vars';});
  536.  
  537. =head1 DESCRIPTION
  538.  
  539. This is a library for generating form letters, building HTML pages, or
  540. filling in templates generally.  A `template' is a piece of text that
  541. has little Perl programs embedded in it here and there.  When you
  542. `fill in' a template, you evaluate the little programs and replace
  543. them with their values.  
  544.  
  545. You can store a template in a file outside your program.  People can
  546. modify the template without modifying the program.  You can separate
  547. the formatting details from the main code, and put the formatting
  548. parts of the program into the template.  That prevents code bloat and
  549. encourages functional separation.
  550.  
  551. =head2 Example
  552.  
  553. Here's an example of a template, which we'll suppose is stored in the
  554. file C<formletter.tmpl>:
  555.  
  556.     Dear {$title} {$lastname},
  557.  
  558.     It has come to our attention that you are delinquent in your
  559.     {$monthname[$last_paid_month]} payment.  Please remit
  560.     ${sprintf("%.2f", $amount)} immediately, or your patellae may
  561.     be needlessly endangered.
  562.  
  563.             Love,
  564.  
  565.             Mark "Vizopteryx" Dominus
  566.  
  567.  
  568. The result of filling in this template is a string, which might look
  569. something like this:
  570.  
  571.     Dear Mr. Gates,
  572.  
  573.     It has come to our attention that you are delinquent in your
  574.     February payment.  Please remit
  575.     $392.12 immediately, or your patellae may
  576.     be needlessly endangered.
  577.  
  578.  
  579.             Love,
  580.  
  581.             Mark "Vizopteryx" Dominus
  582.  
  583. Here is a complete program that transforms the example
  584. template into the example result, and prints it out:
  585.  
  586.     use Text::Template;
  587.  
  588.     my $template = Text::Template->new(SOURCE => 'formletter.tmpl')
  589.       or die "Couldn't construct template: $Text::Template::ERROR";
  590.  
  591.     my @monthname = qw(January February March April May June
  592.                            July August September October November December);
  593.     my %vars = (title => 'Mr.',
  594.             firstname => 'Bill',
  595.             lastname => 'Gates',
  596.             last_paid_month => 1,   # February
  597.             amount => 392.12,
  598.             monthname => \@monthname,
  599.            );
  600.  
  601.     my $result = $template->fill_in(HASH => \%vars);
  602.  
  603.     if (defined $result) { print $result }
  604.     else { die "Couldn't fill in template: $Text::Template::ERROR" }
  605.  
  606.  
  607. =head2 Philosophy
  608.  
  609. When people make a template module like this one, they almost always
  610. start by inventing a special syntax for substitutions.  For example,
  611. they build it so that a string like C<%%VAR%%> is replaced with the
  612. value of C<$VAR>.  Then they realize the need extra formatting, so
  613. they put in some special syntax for formatting.  Then they need a
  614. loop, so they invent a loop syntax.  Pretty soon they have a new
  615. little template language.
  616.  
  617. This approach has two problems: First, their little language is
  618. crippled. If you need to do something the author hasn't thought of,
  619. you lose.  Second: Who wants to learn another language?  You already
  620. know Perl, so why not use it?
  621.  
  622. C<Text::Template> templates are programmed in I<Perl>.  You embed Perl
  623. code in your template, with C<{> at the beginning and C<}> at the end.
  624. If you want a variable interpolated, you write it the way you would in
  625. Perl.  If you need to make a loop, you can use any of the Perl loop
  626. constructions.  All the Perl built-in functions are available.
  627.  
  628. =head1 Details
  629.  
  630. =head2 Template Parsing
  631.  
  632. The C<Text::Template> module scans the template source.  An open brace
  633. C<{> begins a program fragment, which continues until the matching
  634. close brace C<}>.  When the template is filled in, the program
  635. fragments are evaluated, and each one is replaced with the resulting
  636. value to yield the text that is returned.
  637.  
  638. A backslash C<\> in front of a brace (or another backslash that is in
  639. front of a brace) escapes its special meaning.  The result of filling
  640. out this template:
  641.  
  642.     \{ The sum of 1 and 2 is {1+2}  \}
  643.  
  644. is
  645.  
  646.     { The sum of 1 and 2 is 3  }
  647.  
  648. If you have an unmatched brace, C<Text::Template> will return a
  649. failure code and a warning about where the problem is.  Backslashes
  650. that do not precede a brace are passed through unchanged.  If you have
  651. a template like this:
  652.  
  653.     { "String that ends in a newline.\n" }
  654.  
  655. The backslash inside the string is passed through to Perl unchanged,
  656. so the C<\n> really does turn into a newline.  See the note at the end
  657. for details about the way backslashes work.  Backslash processing is
  658. I<not> done when you specify alternative delimiters with the
  659. C<DELIMITERS> option.  (See L<"Alternative Delimiters">, below.)
  660.  
  661. Each program fragment should be a sequence of Perl statements, which
  662. are evaluated the usual way.  The result of the last statement
  663. executed will be evaluted in scalar context; the result of this
  664. statement is a string, which is interpolated into the template in
  665. place of the program fragment itself.
  666.  
  667. The fragments are evaluated in order, and side effects from earlier
  668. fragments will persist into later fragments:
  669.  
  670.     {$x = @things; ''}The Lord High Chamberlain has gotten {$x}
  671.     things for me this year.  
  672.     { $diff = $x - 17; 
  673.       $more = 'more'
  674.       if ($diff == 0) {
  675.         $diff = 'no';
  676.       } elsif ($diff < 0) {
  677.         $more = 'fewer';
  678.       } 
  679.           '';
  680.     } 
  681.     That is {$diff} {$more} than he gave me last year.
  682.  
  683. The value of C<$x> set in the first line will persist into the next
  684. fragment that begins on the third line, and the values of C<$diff> and
  685. C<$more> set in the second fragment will persist and be interpolated
  686. into the last line.  The output will look something like this:
  687.  
  688.     The Lord High Chamberlain has gotten 42
  689.     things for me this year.  
  690.  
  691.     That is 25 more than he gave me last year.
  692.  
  693. That is all the syntax there is.  
  694.  
  695. =head2 The C<$OUT> variable
  696.  
  697. There is one special trick you can play in a template.  Here is the
  698. motivation for it:  Suppose you are going to pass an array, C<@items>,
  699. into the template, and you want the template to generate a bulleted
  700. list with a header, like this:
  701.  
  702.     Here is a list of the things I have got for you since 1907:
  703.       * Ivory
  704.       * Apes
  705.       * Peacocks
  706.       * ...
  707.  
  708. One way to do it is with a template like this:
  709.  
  710.     Here is a list of the things I have got for you since 1907:
  711.     { my $blist = '';
  712.           foreach $i (@items) {
  713.             $blist .= qq{  * $i\n};
  714.           }    
  715.           $blist;
  716.         } 
  717.  
  718. Here we construct the list in a variable called C<$blist>, which we
  719. return at the end.  This is a little cumbersome.  There is a shortcut.
  720.  
  721. Inside of templates, there is a special variable called C<$OUT>.
  722. Anything you append to this variable will appear in the output of the
  723. template.  Also, if you use C<$OUT> in a program fragment, the normal
  724. behavior, of replacing the fragment with its return value, is
  725. disabled; instead the fragment is replaced with the value of C<$OUT>.
  726. This means that you can write the template above like this:
  727.  
  728.     Here is a list of the things I have got for you since 1907:
  729.     { foreach $i (@items) {
  730.             $OUT .= "  * $i\n";
  731.           }    
  732.         } 
  733.  
  734. C<$OUT> is reinitialized to the empty string at the start of each
  735. program fragment.  It is private to C<Text::Template>, so 
  736. you can't use a variable named C<$OUT> in your template without
  737. invoking the special behavior.
  738.  
  739. =head2 General Remarks
  740.  
  741. All C<Text::Template> functions return C<undef> on failure, and set the
  742. variable C<$Text::Template::ERROR> to contain an explanation of what
  743. went wrong.  For example, if you try to create a template from a file
  744. that does not exist, C<$Text::Template::ERROR> will contain something like:
  745.  
  746.     Couldn't open file xyz.tmpl: No such file or directory
  747.  
  748. =head2 C<new>
  749.  
  750.     $template = new Text::Template ( TYPE => ..., SOURCE => ... );
  751.  
  752. This creates and returns a new template object.  C<new> returns
  753. C<undef> and sets C<$Text::Template::ERROR> if it can't create the
  754. template object.  C<SOURCE> says where the template source code will
  755. come from.  C<TYPE> says what kind of object the source is.
  756.  
  757. The most common type of source is a file:
  758.  
  759.     new Text::Template ( TYPE => 'FILE', SOURCE => $filename );
  760.  
  761. This reads the template from the specified file.  The filename is
  762. opened with the Perl C<open> command, so it can be a pipe or anything
  763. else that makes sense with C<open>.
  764.  
  765. The C<TYPE> can also be C<STRING>, in which case the C<SOURCE> should
  766. be a string:
  767.  
  768.     new Text::Template ( TYPE => 'STRING', 
  769.                              SOURCE => "This is the actual template!" );
  770.  
  771. The C<TYPE> can be C<ARRAY>, in which case the source should be a
  772. reference to an array of strings.  The concatenation of these strings
  773. is the template:
  774.  
  775.     new Text::Template ( TYPE => 'ARRAY', 
  776.                              SOURCE => [ "This is ", "the actual", 
  777.                                          " template!",
  778.                                        ]
  779.                            );
  780.  
  781. The C<TYPE> can be FILEHANDLE, in which case the source should be an
  782. open filehandle (such as you got from the C<FileHandle> or C<IO::*>
  783. packages, or a glob, or a reference to a glob).  In this case
  784. C<Text::Template> will read the text from the filehandle up to
  785. end-of-file, and that text is the template:
  786.  
  787.     # Read template source code from STDIN:
  788.     new Text::Template ( TYPE => 'FILEHANDLE', 
  789.                              SOURCE => \*STDIN  );
  790.  
  791.  
  792. If you omit the C<TYPE> attribute, it's taken to be C<FILE>.
  793. C<SOURCE> is required.  If you omit it, the program will abort.
  794.  
  795. The words C<TYPE> and C<SOURCE> can be spelled any of the following ways:
  796.  
  797.     TYPE    SOURCE
  798.     Type    Source
  799.     type    source
  800.     -TYPE    -SOURCE
  801.     -Type    -Source
  802.     -type    -source
  803.  
  804. Pick a style you like and stick with it.
  805.  
  806. =over 4
  807.  
  808. =item C<DELIMITERS>
  809.  
  810. You may also add a C<DELIMITERS> option.  If this option is present,
  811. its value should be a reference to an array of two strings.  The first
  812. string is the string that signals the beginning of each program
  813. fragment, and the second string is the string that signals the end of
  814. each program fragment.  See L<"Alternative Delimiters">, below.
  815.  
  816. =item C<UNTAINT>
  817.  
  818. If your program is running in taint mode, you may have problems if
  819. your templates are stored in files.  Data read from files is
  820. considered 'untrustworthy', and taint mode will not allow you to
  821. evaluate the Perl code in the file.  (It is afraid that a malicious
  822. person might have tampered with the file.)
  823.  
  824. In some environments, however, local files are trustworthy.  You can
  825. tell C<Text::Template> that a certain file is trustworthy by supplying
  826. C<UNTAINT =E<gt> 1> in the call to C<new>.  This will tell
  827. C<Text::Template> to disable taint checks on template code that has
  828. come from a file, as long as the filename itself is considered
  829. trustworthy.  It will also disable taint checks on template code that
  830. comes from a filehandle.  When used with C<TYPE =E<gt> 'string'> or C<TYPE
  831. =E<gt> 'array'>, it has no effect.
  832.  
  833. See L<perlsec> for more complete information about tainting.
  834.  
  835. Thanks to Steve Palincsar, Gerard Vreeswijk, and Dr. Christoph Baehr
  836. for help with this feature.
  837.  
  838. =item C<PREPEND>
  839.  
  840. This option is passed along to the C<fill_in> call unless it is
  841. overridden in the arguments to C<fill_in>.  See L<C<PREPEND> feature
  842. and using C<strict> in templates> below.
  843.  
  844. =item C<BROKEN>
  845.  
  846. This option is passed along to the C<fill_in> call unless it is
  847. overridden in the arguments to C<fill_in>.  See L<C<BROKEN>> below.
  848.  
  849. =back
  850.  
  851. =head2 C<compile>
  852.  
  853.     $template->compile()
  854.  
  855. Loads all the template text from the template's source, parses and
  856. compiles it.  If successful, returns true; otherwise returns false and
  857. sets C<$Text::Template::ERROR>.  If the template is already compiled,
  858. it returns true and does nothing.  
  859.  
  860. You don't usually need to invoke this function, because C<fill_in>
  861. (see below) compiles the template if it isn't compiled already.
  862.  
  863. If there is an argument to this function, it must be a reference to an
  864. array containing alternative delimiter strings.  See C<"Alternative
  865. Delimiters">, below.
  866.  
  867. =head2 C<fill_in>
  868.  
  869.     $template->fill_in(OPTIONS);
  870.  
  871. Fills in a template.  Returns the resulting text if successful.
  872. Otherwise, returns C<undef>  and sets C<$Text::Template::ERROR>.
  873.  
  874. The I<OPTIONS> are a hash, or a list of key-value pairs.  You can
  875. write the key names in any of the six usual styles as above; this
  876. means that where this manual says C<PACKAGE> (for example) you can
  877. actually use any of
  878.  
  879.     PACKAGE Package package -PACKAGE -Package -package
  880.  
  881. Pick a style you like and stick with it.  The all-lowercase versions
  882. may yield spurious warnings about
  883.  
  884.     Ambiguous use of package => resolved to "package"
  885.  
  886. so you might like to avoid them and use the capitalized versions.
  887.  
  888. At present, there are eight legal options:  C<PACKAGE>, C<BROKEN>,
  889. C<BROKEN_ARG>, C<SAFE>, C<HASH>, C<OUTPUT>, and C<DELIMITERS>.
  890.  
  891. =over 4
  892.  
  893. =item C<PACKAGE>
  894.  
  895. C<PACKAGE> specifies the name of a package in which the program
  896. fragments should be evaluated.  The default is to use the package from
  897. which C<fill_in> was called.  For example, consider this template:
  898.  
  899.     The value of the variable x is {$x}.
  900.  
  901. If you use C<$template-E<gt>fill_in(PACKAGE =E<gt> 'R')> , then the C<$x> in
  902. the template is actually replaced with the value of C<$R::x>.  If you
  903. omit the C<PACKAGE> option, C<$x> will be replaced with the value of
  904. the C<$x> variable in the package that actually called C<fill_in>.
  905.  
  906. You should almost always use C<PACKAGE>.  If you don't, and your
  907. template makes changes to variables, those changes will be propagated
  908. back into the main program.  Evaluating the template in a private
  909. package helps prevent this.  The template can still modify variables
  910. in your program if it wants to, but it will have to do so explicitly.
  911. See the section at the end on `Security'.
  912.  
  913. Here's an example of using C<PACKAGE>:
  914.  
  915.     Your Royal Highness,
  916.  
  917.     Enclosed please find a list of things I have gotten
  918.     for you since 1907:
  919.  
  920.     { foreach $item (@items) {
  921.             $item_no++;
  922.         $OUT .= " $item_no. \u$item\n";
  923.       }
  924.     }
  925.  
  926.     Signed,
  927.     Lord High Chamberlain
  928.  
  929. We want to pass in an array which will be assigned to the array
  930. C<@items>.  Here's how to do that:
  931.  
  932.  
  933.     @items = ('ivory', 'apes', 'peacocks', );
  934.     $template->fill_in();
  935.  
  936. This is not very safe.  The reason this isn't as safe is that if you
  937. had a variable named C<$item_no> in scope in your program at the point
  938. you called C<fill_in>, its value would be clobbered by the act of
  939. filling out the template.  The problem is the same as if you had
  940. written a subroutine that used those variables in the same way that
  941. the template does.  (C<$OUT> is special in templates and is always
  942. safe.)
  943.  
  944. One solution to this is to make the C<$item_no> variable private to the
  945. template by declaring it with C<my>.  If the template does this, you
  946. are safe.
  947.  
  948. But if you use the C<PACKAGE> option, you will probably be safe even
  949. if the template does I<not> declare its variables with C<my>:
  950.  
  951.     @Q::items = ('ivory', 'apes', 'peacocks', );
  952.     $template->fill_in(PACKAGE => 'Q');
  953.  
  954. In this case the template will clobber the variable C<$Q::item_no>,
  955. which is not related to the one your program was using.
  956.  
  957. Templates cannot affect variables in the main program that are
  958. declared with C<my>, unless you give the template references to those
  959. variables.
  960.  
  961. =item C<HASH>
  962.  
  963. You may not want to put the template variables into a package.
  964. Packages can be hard to manage:  You can't copy them, for example.
  965. C<HASH> provides an alternative.  
  966.  
  967. The value for C<HASH> should be a reference to a hash that maps
  968. variable names to values.  For example, 
  969.  
  970.     $template->fill_in(HASH => { recipient => "The King",
  971.                      items => ['gold', 'frankincense', 'myrrh']
  972.                    });
  973.  
  974. will fill out the template and use C<"The King"> as the value of
  975. C<$recipient> and the list of items as the value of C<@items>.
  976.  
  977. The full details of how it works are a little involved, so you might
  978. want to skip to the next section.
  979.  
  980. Suppose the key in the hash is I<key> and the value is I<value>.  
  981.  
  982. =over 4
  983.  
  984. =item *
  985.  
  986. If the I<value> is C<undef>, then any variables named C<$key>,
  987. C<@key>, C<%key>, etc., are undefined.  
  988.  
  989. =item *
  990.  
  991. If the I<value> is a string or a number, then C<$key> is set to that
  992. value in the template.
  993.  
  994. =item *
  995.  
  996. If the I<value> is a reference to an array, then C<@key> is set to
  997. that array.  If the I<value> is a reference to a hash, then C<%key> is
  998. set to that hash.  Similarly if I<value> is any other kind of
  999. reference.  This means that
  1000.  
  1001.     var => "foo"
  1002.  
  1003. and
  1004.  
  1005.     var => \"foo"
  1006.  
  1007. have almost exactly the same effect.  (The difference is that in the
  1008. former case, the value is copied, and in the latter case it is
  1009. aliased.)  
  1010.  
  1011. =back
  1012.  
  1013. Normally, the way this works is by allocating a private package,
  1014. loading all the variables into the package, and then filling out the
  1015. template as if you had specified that package.  A new package is
  1016. allocated each time.  However, if you I<also> use the C<PACKAGE>
  1017. option, C<Text::Template> loads the variables into the package you
  1018. specified, and they stay there after the call returns.  Subsequent
  1019. calls to C<fill_in> that use the same package will pick up the values
  1020. you loaded in.
  1021.  
  1022. If the argument of C<HASH> is a reference to an array instead of a
  1023. reference to a hash, then the array should contain a list of hashes
  1024. whose contents are loaded into the template package one after the
  1025. other.  You can use this feature if you want to combine several sets
  1026. of variables.  For example, one set of variables might be the defaults
  1027. for a fill-in form, and the second set might be the user inputs, which
  1028. override the defaults when they are present:
  1029.  
  1030.     $template->fill_in(HASH => [\%defaults, \%user_input]);
  1031.  
  1032. You can also use this to set two variables with the same name:
  1033.  
  1034.     $template->fill_in(HASH => [{ v => "The King" },
  1035.                                     { v => [1,2,3] },
  1036.                                ]
  1037.                           );
  1038.  
  1039. This sets C<$v> to C<"The King"> and C<@v> to C<(1,2,3)>.    
  1040.  
  1041. =item C<BROKEN>
  1042.  
  1043. If any of the program fragments fails to compile or aborts for any
  1044. reason, and you have set the C<BROKEN> option to a function reference,
  1045. C<Text::Template> will invoke the function.  This function is called
  1046. the I<C<BROKEN> function>.  The C<BROKEN> function will tell
  1047. C<Text::Template> what to do next.  
  1048.  
  1049. If the C<BROKEN> function returns C<undef>, C<Text::Template> will
  1050. immediately abort processing the template and return the text that it
  1051. has accumulated so far.  If your function does this, it should set a
  1052. flag that you can examine after C<fill_in> returns so that you can
  1053. tell whether there was a premature return or not. 
  1054.  
  1055. If the C<BROKEN> function returns any other value, that value will be
  1056. interpolated into the template as if that value had been the return
  1057. value of the program fragment to begin with.  For example, if the
  1058. C<BROKEN> function returns an error string, the error string will be
  1059. interpolated into the output of the template in place of the program
  1060. fragment that cased the error.
  1061.  
  1062. If you don't specify a C<BROKEN> function, C<Text::Template> supplies
  1063. a default one that returns something like
  1064.  
  1065.     Program fragment delivered error ``Illegal division by 0 at
  1066.     template line 37''
  1067.  
  1068. (Note that the format of this message has changed slightly since
  1069. version 1.31.)  The return value of the C<BROKEN> function is
  1070. interpolated into the template at the place the error occurred, so
  1071. that this template:
  1072.  
  1073.     (3+4)*5 = { 3+4)*5 }
  1074.  
  1075. yields this result:
  1076.  
  1077.     (3+4)*5 = Program fragment delivered error ``syntax error at template line 1''
  1078.  
  1079. If you specify a value for the C<BROKEN> attribute, it should be a
  1080. reference to a function that C<fill_in> can call instead of the
  1081. default function.
  1082.  
  1083. C<fill_in> will pass a hash to the C<broken> function.
  1084. The hash will have at least these three members:
  1085.  
  1086. =over 4
  1087.  
  1088. =item C<text>
  1089.  
  1090. The source code of the program fragment that failed
  1091.  
  1092. =item C<error>
  1093.  
  1094. The text of the error message (C<$@>) generated by eval.
  1095.  
  1096. The text has been modified to omit the trailing newline and to include
  1097. the name of the template file (if there was one).  The line number
  1098. counts from the beginning of the template, not from the beginning of
  1099. the failed program fragment.
  1100.  
  1101. =item C<lineno>
  1102.  
  1103. The line number of the template at which the program fragment began.
  1104.  
  1105. =back
  1106.  
  1107. There may also be an C<arg> member.  See C<BROKEN_ARG>, below
  1108.  
  1109. =item C<BROKEN_ARG>
  1110.  
  1111. If you supply the C<BROKEN_ARG> option to C<fill_in>, the value of the
  1112. option is passed to the C<BROKEN> function whenever it is called.  The
  1113. default C<BROKEN> function ignores the C<BROKEN_ARG>, but you can
  1114. write a custom C<BROKEN> function that uses the C<BROKEN_ARG> to get
  1115. more information about what went wrong. 
  1116.  
  1117. The C<BROKEN> function could also use the C<BROKEN_ARG> as a reference
  1118. to store an error message or some other information that it wants to
  1119. communicate back to the caller.  For example:
  1120.  
  1121.     $error = '';
  1122.  
  1123.     sub my_broken {    
  1124.        my %args = @_;
  1125.        my $err_ref = $args{arg};
  1126.        ...
  1127.        $$err_ref = "Some error message";
  1128.        return undef;
  1129.     }
  1130.  
  1131.     $template->fill_in(BROKEN => \&my_broken,
  1132.                BROKEN_ARG => \$error,
  1133.               );
  1134.  
  1135.     if ($error) {
  1136.       die "It didn't work: $error";
  1137.     }
  1138.  
  1139. If one of the program fragments in the template fails, it will call
  1140. the C<BROKEN> function, C<my_broken>, and pass it the C<BROKEN_ARG>,
  1141. which is a reference to C<$error>.  C<my_broken> can store an error
  1142. message into C<$error> this way.  Then the function that called
  1143. C<fill_in> can see if C<my_broken> has left an error message for it
  1144. to find, and proceed accordingly.
  1145.  
  1146. =item C<SAFE>
  1147.  
  1148. If you give C<fill_in> a C<SAFE> option, its value should be a safe
  1149. compartment object from the C<Safe> package.  All evaluation of
  1150. program fragments will be performed in this compartment.  See L<Safe>
  1151. for full details about such compartments and how to restrict the
  1152. operations that can be performed in them.
  1153.  
  1154. If you use the C<PACKAGE> option with C<SAFE>, the package you specify
  1155. will be placed into the safe compartment and evaluation will take
  1156. place in that package as usual.  
  1157.  
  1158. If not, C<SAFE> operation is a little different from the default.
  1159. Usually, if you don't specify a package, evaluation of program
  1160. fragments occurs in the package from which the template was invoked.
  1161. But in C<SAFE> mode the evaluation occurs inside the safe compartment
  1162. and cannot affect the calling package.  Normally, if you use C<HASH>
  1163. without C<PACKAGE>, the hash variables are imported into a private,
  1164. one-use-only package.  But if you use C<HASH> and C<SAFE> together
  1165. without C<PACKAGE>, the hash variables will just be loaded into the
  1166. root namespace of the C<Safe> compartment.
  1167.  
  1168. =item C<OUTPUT>
  1169.  
  1170. If your template is going to generate a lot of text that you are just
  1171. going to print out again anyway,  you can save memory by having
  1172. C<Text::Template> print out the text as it is generated instead of
  1173. making it into a big string and returning the string.  If you supply
  1174. the C<OUTPUT> option to C<fill_in>, the value should be a filehandle.
  1175. The generated text will be printed to this filehandle as it is
  1176. constructed.  For example:
  1177.  
  1178.     $template->fill_in(OUTPUT => \*STDOUT, ...);
  1179.  
  1180. fills in the C<$template> as usual, but the results are immediately
  1181. printed to STDOUT.  This may result in the output appearing more
  1182. quickly than it would have otherwise.
  1183.  
  1184. If you use C<OUTPUT>, the return value from C<fill_in> is still true on
  1185. success and false on failure, but the complete text is not returned to
  1186. the caller.
  1187.  
  1188. =item C<PREPEND>
  1189.  
  1190. You can have some Perl code prepended automatically to the beginning
  1191. of every program fragment.  See L<C<PREPEND> feature and using
  1192. C<strict> in templates> below.
  1193.  
  1194. =item C<DELIMITERS>
  1195.  
  1196. If this option is present, its value should be a reference to a list
  1197. of two strings.  The first string is the string that signals the
  1198. beginning of each program fragment, and the second string is the
  1199. string that signals the end of each program fragment.  See
  1200. L<"Alternative Delimiters">, below.  
  1201.  
  1202. If you specify C<DELIMITERS> in the call to C<fill_in>, they override
  1203. any delimiters you set when you created the template object with
  1204. C<new>. 
  1205.  
  1206. =back
  1207.  
  1208. =head1 Convenience Functions
  1209.  
  1210. =head2 C<fill_this_in>
  1211.  
  1212. The basic way to fill in a template is to create a template object and
  1213. then call C<fill_in> on it.   This is useful if you want to fill in
  1214. the same template more than once.
  1215.  
  1216. In some programs, this can be cumbersome.  C<fill_this_in> accepts a
  1217. string, which contains the template, and a list of options, which are
  1218. passed to C<fill_in> as above.  It constructs the template object for
  1219. you, fills it in as specified, and returns the results.  It returns
  1220. C<undef> and sets C<$Text::Template::ERROR> if it couldn't generate
  1221. any results.
  1222.  
  1223. An example:
  1224.  
  1225.     $Q::name = 'Donald';
  1226.     $Q::amount = 141.61;
  1227.     $Q::part = 'hyoid bone';
  1228.  
  1229.     $text = Text::Template->fill_this_in( <<'EOM', PACKAGE => Q);
  1230.     Dear {$name},
  1231.     You owe me \\${sprintf('%.2f', $amount)}.  
  1232.     Pay or I will break your {$part}.
  1233.         Love,
  1234.         Grand Vizopteryx of Irkutsk.
  1235.     EOM
  1236.  
  1237. Notice how we included the template in-line in the program by using a
  1238. `here document' with the C<E<lt>E<lt>> notation.
  1239.  
  1240. C<fill_this_in> is a deprecated feature.  It is only here for
  1241. backwards compatibility, and may be removed in some far-future version
  1242. in C<Text::Template>.  You should use C<fill_in_string> instead.  It
  1243. is described in the next section.
  1244.  
  1245. =head2 C<fill_in_string>
  1246.  
  1247. It is stupid that C<fill_this_in> is a class method.  It should have
  1248. been just an imported function, so that you could omit the
  1249. C<Text::Template-E<gt>> in the example above.  But I made the mistake
  1250. four years ago and it is too late to change it.
  1251.  
  1252. C<fill_in_string> is exactly like C<fill_this_in> except that it is
  1253. not a method and you can omit the C<Text::Template-E<gt>> and just say
  1254.  
  1255.     print fill_in_string(<<'EOM', ...);
  1256.     Dear {$name},
  1257.       ...
  1258.     EOM
  1259.  
  1260. To use C<fill_in_string>, you need to say
  1261.  
  1262.     use Text::Template 'fill_in_string';
  1263.  
  1264. at the top of your program.   You should probably use
  1265. C<fill_in_string> instead of C<fill_this_in>.
  1266.  
  1267. =head2 C<fill_in_file>
  1268.  
  1269. If you import C<fill_in_file>, you can say
  1270.  
  1271.     $text = fill_in_file(filename, ...);
  1272.  
  1273. The C<...> are passed to C<fill_in> as above.  The filename is the
  1274. name of the file that contains the template you want to fill in.  It
  1275. returns the result text. or C<undef>, as usual.
  1276.  
  1277. If you are going to fill in the same file more than once in the same
  1278. program you should use the longer C<new> / C<fill_in> sequence instead.
  1279. It will be a lot faster because it only has to read and parse the file
  1280. once.
  1281.  
  1282. =head2 Including files into templates
  1283.  
  1284. People always ask for this.  ``Why don't you have an include
  1285. function?'' they want to know.  The short answer is this is Perl, and
  1286. Perl already has an include function.  If you want it, you can just put
  1287.  
  1288.     {qx{cat filename}}
  1289.  
  1290. into your template.  VoilE<agrave>.
  1291.  
  1292. If you don't want to use C<cat>, you can write a little four-line
  1293. function that opens a file and dumps out its contents, and call it
  1294. from the template.  I wrote one for you.  In the template, you can say
  1295.  
  1296.     {Text::Template::_load_text(filename)}
  1297.  
  1298. If that is too verbose, here is a trick.  Suppose the template package
  1299. that you are going to be mentioning in the C<fill_in> call is package
  1300. C<Q>.  Then in the main program, write
  1301.  
  1302.     *Q::include = \&Text::Template::_load_text;
  1303.  
  1304. This imports the C<_load_text> function into package C<Q> with the
  1305. name C<include>.  From then on, any template that you fill in with
  1306. package C<Q> can say
  1307.  
  1308.     {include(filename)}
  1309.  
  1310. to insert the text from the named file at that point.  If you are
  1311. using the C<HASH> option instead, just put C<include =E<gt>
  1312. \&Text::Template::_load_text> into the hash instead of importing it
  1313. explicitly.
  1314.  
  1315. Suppose you don't want to insert a plain text file, but rather you
  1316. want to include one template within another?  Just use C<fill_in_file>
  1317. in the template itself:
  1318.  
  1319.     {Text::Template::fill_in_file(filename)}
  1320.  
  1321. You can do the same importing trick if this is too much to type.
  1322.  
  1323. =head1 Miscellaneous
  1324.  
  1325. =head2 C<my> variables
  1326.  
  1327. People are frequently surprised when this doesn't work:
  1328.  
  1329.     my $recipient = 'The King';
  1330.     my $text = fill_in_file('formletter.tmpl');
  1331.  
  1332. The text C<The King> doesn't get into the form letter.  Why not?
  1333. Because C<$recipient> is a C<my> variable, and the whole point of
  1334. C<my> variables is that they're private and inaccessible except in the
  1335. scope in which they're declared.  The template is not part of that
  1336. scope, so the template can't see C<$recipient>.  
  1337.  
  1338. If that's not the behavior you want, don't use C<my>.  C<my> means a
  1339. private variable, and in this case you don't want the variable to be
  1340. private.  Put the variables into package variables in some other
  1341. package, and use the C<PACKAGE> option to C<fill_in>:
  1342.  
  1343.     $Q::recipient = $recipient;
  1344.     my $text = fill_in_file('formletter.tmpl', PACKAGE => 'Q');
  1345.     
  1346.  
  1347. or pass the names and values in a hash with the C<HASH> option:
  1348.  
  1349.     my $text = fill_in_file('formletter.tmpl', HASH => { recipient => $recipient });
  1350.  
  1351. =head2 Security Matters
  1352.  
  1353. All variables are evaluated in the package you specify with the
  1354. C<PACKAGE> option of C<fill_in>.  if you use this option, and if your
  1355. templates don't do anything egregiously stupid, you won't have to
  1356. worry that evaluation of the little programs will creep out into the
  1357. rest of your program and wreck something.
  1358.  
  1359. Nevertheless, there's really no way (except with C<Safe>) to protect
  1360. against a template that says
  1361.  
  1362.     { $Important::Secret::Security::Enable = 0; 
  1363.       # Disable security checks in this program 
  1364.     }
  1365.  
  1366. or
  1367.  
  1368.     { $/ = "ho ho ho";   # Sabotage future uses of <FH>.
  1369.       # $/ is always a global variable
  1370.     }
  1371.  
  1372. or even
  1373.  
  1374.     { system("rm -rf /") }
  1375.  
  1376. so B<don't> go filling in templates unless you're sure you know what's
  1377. in them.  If you're worried, or you can't trust the person who wrote
  1378. the template, use the C<SAFE> option.
  1379.  
  1380. A final warning: program fragments run a small risk of accidentally
  1381. clobbering local variables in the C<fill_in> function itself.  These
  1382. variables all have names that begin with C<$fi_>, so if you stay away
  1383. from those names you'll be safe.  (Of course, if you're a real wizard
  1384. you can tamper with them deliberately for exciting effects; this is
  1385. actually how C<$OUT> works.)  I can fix this, but it will make the
  1386. package slower to do it, so I would prefer not to.  If you are worried
  1387. about this, send me mail and I will show you what to do about it.
  1388.  
  1389. =head2 Alternative Delimiters
  1390.  
  1391. Lorenzo Valdettaro pointed out that if you are using C<Text::Template>
  1392. to generate TeX output, the choice of braces as the program fragment
  1393. delimiters makes you suffer suffer suffer.  Starting in version 1.20,
  1394. you can change the choice of delimiters to something other than curly
  1395. braces.
  1396.  
  1397. In either the C<new()> call or the C<fill_in()> call, you can specify
  1398. an alternative set of delimiters with the C<DELIMITERS> option.  For
  1399. example, if you would like code fragments to be delimited by C<[@-->
  1400. and C<--@]> instead of C<{> and C<}>, use
  1401.  
  1402.     ... DELIMITERS => [ '[@--', '--@]' ], ...
  1403.  
  1404. Note that these delimiters are I<literal strings>, not regexes.  (I
  1405. tried for regexes, but it complicates the lexical analysis too much.)
  1406. Note also that C<DELIMITERS> disables the special meaning of the
  1407. backslash, so if you want to include the delimiters in the literal
  1408. text of your template file, you are out of luck---it is up to you to
  1409. choose delimiters that do not conflict with what you are doing.  The
  1410. delimiter strings may still appear inside of program fragments as long
  1411. as they nest properly.  This means that if for some reason you
  1412. absolutely must have a program fragment that mentions one of the
  1413. delimiters, like this:
  1414.  
  1415.     [@--
  1416.         print "Oh no, a delimiter: --@]\n"
  1417.     --@]
  1418.  
  1419. you may be able to make it work by doing this instead:
  1420.  
  1421.     [@--
  1422.         # Fake matching delimiter in a comment: [@--
  1423.         print "Oh no, a delimiter: --@]\n"
  1424.     --@]
  1425.  
  1426. It may be safer to choose delimiters that begin with a newline
  1427. character.  
  1428.  
  1429. Because the parsing of templates is simplified by the absence of
  1430. backslash escapes, using alternative C<DELIMITERS> I<speeds up> the
  1431. parsing process by 20-25%.  This shows that my original choice of C<{>
  1432. and C<}> was very bad.  I therefore recommend that you use alternative
  1433. delimiters whenever possible. 
  1434.  
  1435. =head2 C<PREPEND> feature and using C<strict> in templates
  1436.  
  1437. Suppose you would like to use C<strict> in your templates to detect
  1438. undeclared variables and the like.  But each code fragment is a
  1439. separate lexical scope, so you have to turn on C<strict> at the top of
  1440. each and every code fragment:
  1441.  
  1442.     { use strict;
  1443.       use vars '$foo';
  1444.       $foo = 14;
  1445.       ...
  1446.     }
  1447.  
  1448.     ...
  1449.  
  1450.     { # we forgot to put `use strict' here
  1451.       my $result = $boo + 12;    # $boo is misspelled and should be $foo
  1452.       # No error is raised on `$boo'
  1453.     }
  1454.  
  1455. Because we didn't put C<use strict> at the top of the second fragment,
  1456. it was only active in the first fragment, and we didn't get any
  1457. C<strict> checking in the second fragment.  Then we mispelled C<$foo>
  1458. and the error wasn't caught.  
  1459.  
  1460. C<Text::Template> version 1.22 and higher has a new feature to make
  1461. this easier.  You can specify that any text at all be automatically
  1462. added to the beginning of each program fragment.  
  1463.  
  1464. When you make a call to C<fill_in>, you can specify a
  1465.  
  1466.     PREPEND => 'some perl statements here'
  1467.  
  1468. option; the statements will be prepended to each program fragment for
  1469. that one call only.  Suppose that the C<fill_in> call included a
  1470.  
  1471.     PREPEND => 'use strict;'
  1472.  
  1473. option, and that the template looked like this:
  1474.  
  1475.     { use vars '$foo';
  1476.       $foo = 14;
  1477.       ...
  1478.     }
  1479.  
  1480.     ...
  1481.  
  1482.     { my $result = $boo + 12;    # $boo is misspelled and should be $foo
  1483.       ...
  1484.     }
  1485.  
  1486. The code in the second fragment would fail, because C<$boo> has not
  1487. been declared.  C<use strict> was implied, even though you did not
  1488. write it explicitly, because the C<PREPEND> option added it for you
  1489. automatically.
  1490.  
  1491. There are two other ways to do this.  At the time you create the
  1492. template object with C<new>, you can also supply a C<PREPEND> option,
  1493. in which case the statements will be prepended each time you fill in
  1494. that template.  If the C<fill_in> call has its own C<PREPEND> option,
  1495. this overrides the one specified at the time you created the
  1496. template.  Finally, you can make the class method call
  1497.  
  1498.     Text::Template->always_prepend('perl statements');
  1499.  
  1500. If you do this, then call calls to C<fill_in> for I<any> template will
  1501. attach the perl statements to the beginning of each program fragment,
  1502. except where overridden by C<PREPEND> options to C<new> or C<fill_in>.
  1503.  
  1504. =head2 Prepending in Derived Classes
  1505.  
  1506. This section is technical, and you should skip it on the first few
  1507. readings. 
  1508.  
  1509. Normally there are three places that prepended text could come from.
  1510. It could come from the C<PREPEND> option in the C<fill_in> call, from
  1511. the C<PREPEND> option in the C<new> call that created the template
  1512. object, or from the argument of the C<always_prepend> call.
  1513. C<Text::Template> looks for these three things in order and takes the
  1514. first one that it finds.
  1515.  
  1516. In a subclass of C<Text::Template>, this last possibility is
  1517. ambiguous.  Suppose C<S> is a subclass of C<Text::Template>.  Should 
  1518.  
  1519.     Text::Template->always_prepend(...);
  1520.  
  1521. affect objects in class C<Derived>?  The answer is that you can have it
  1522. either way.  
  1523.  
  1524. The C<always_prepend> value for C<Text::Template> is normally stored
  1525. in  a hash variable named C<%GLOBAL_PREPEND> under the key
  1526. C<Text::Template>.  When C<Text::Template> looks to see what text to
  1527. prepend, it first looks in the template object itself, and if not, it
  1528. looks in C<$GLOBAL_PREPEND{I<class>}> where I<class> is the class to
  1529. which the template object belongs.  If it doesn't find any value, it
  1530. looks in C<$GLOBAL_PREPEND{'Text::Template'}>.  This means that
  1531. objects in class C<Derived> I<will> be affected by
  1532.  
  1533.     Text::Template->always_prepend(...);
  1534.  
  1535. I<unless> there is also a call to
  1536.  
  1537.     Derived->always_prepend(...);
  1538.  
  1539. So when you're designing your derived class, you can arrange to have
  1540. your objects ignore C<Text::Template::always_prepend> calls by simply
  1541. putting C<Derived-E<gt>always_prepend('')> at the top of your module.
  1542.  
  1543. Of course, there is also a final escape hatch: Templates support a
  1544. C<prepend_text> that is used to look up the appropriate text to be
  1545. prepended at C<fill_in> time.  Your derived class can override this
  1546. method to get an arbitrary effect.
  1547.  
  1548. =head2 JavaScript
  1549.  
  1550. Jennifer D. St Clair asks:
  1551.  
  1552.     > Most of my pages contain JavaScript and Stylesheets.
  1553.         > How do I change the template identifier?  
  1554.  
  1555. Jennifer is worried about the braces in the JavaScript being taken as
  1556. the delimiters of the Perl program fragments.  Of course, disaster
  1557. will ensue when perl tries to evaluate these as if they were Perl
  1558. programs.  The best choice is to find some unambiguous delimiter
  1559. strings that you can use in your template instead of curly braces, and
  1560. then use the C<DELIMITERS> option.  However, if you can't do this for
  1561. some reason, there are  two easy workarounds:
  1562.  
  1563. 1. You can put C<\> in front of C<{>, C<}>, or C<\> to remove its
  1564. special meaning.  So, for example, instead of
  1565.  
  1566.         if (br== "n3") { 
  1567.         // etc.
  1568.         }
  1569.  
  1570. you can put
  1571.  
  1572.         if (br== "n3") \{ 
  1573.         // etc.
  1574.         \}
  1575.  
  1576. and it'll come out of the template engine the way you want.
  1577.  
  1578. But here is another method that is probably better.  To see how it
  1579. works, first consider what happens if you put this into a template:
  1580.  
  1581.         { 'foo' }
  1582.  
  1583. Since it's in braces, it gets evaluated, and obviously, this is going
  1584. to turn into
  1585.  
  1586.         foo
  1587.  
  1588. So now here's the trick: In Perl, C<q{...}> is the same as C<'...'>.
  1589. So if we wrote
  1590.  
  1591.         {q{foo}}
  1592.  
  1593. it would turn into 
  1594.  
  1595.         foo
  1596.  
  1597. So for your JavaScript, just write
  1598.  
  1599.         {q{if (br== "n3") { 
  1600.            // etc.
  1601.            }}
  1602.         }
  1603.  
  1604. and it'll come out as
  1605.  
  1606.           if (br== "n3") { 
  1607.             // etc.
  1608.           }
  1609.  
  1610. which is what you want.
  1611.  
  1612.  
  1613. =head2 Shut Up!
  1614.  
  1615. People sometimes try to put an initialization section at the top of
  1616. their templates, like this:
  1617.  
  1618.     { ...
  1619.       $var = 17;
  1620.     }
  1621.  
  1622. Then they complain because there is a C<17> at the top of the output
  1623. that they didn't want to have there.  
  1624.  
  1625. Remember that a program fragment is replaced with its own return
  1626. value, and that in Perl the return value of a code block is the value
  1627. of the last expression that was evaluated, which in this case is 17.
  1628. If it didn't do that, you wouldn't be able to write C<{$recipient}>
  1629. and have the recipient filled in.
  1630.  
  1631. To prevent the 17 from appearing in the output is very simple:
  1632.  
  1633.     { ...
  1634.       $var = 17;
  1635.       '';
  1636.     }
  1637.  
  1638. Now the last expression evaluated yields the empty string, which is
  1639. invisible.  If you don't like the way this looks, use
  1640.  
  1641.     { ...
  1642.       $var = 17;
  1643.       ($SILENTLY);
  1644.     }
  1645.  
  1646. instead.  Presumably, C<$SILENTLY> has no value, so nothing will be
  1647. interpolated.  This is what is known as a `trick'.
  1648.  
  1649. =head2 Compatibility
  1650.  
  1651. Every effort has been made to make this module compatible with older
  1652. versions.  The only known exceptions follow:
  1653.  
  1654. The output format of the default C<BROKEN> subroutine has changed
  1655. twice, most recently between versions 1.31 and 1.40.
  1656.  
  1657. Starting in version 1.10, the C<$OUT> variable is arrogated for a
  1658. special meaning.  If you had templates before version 1.10 that
  1659. happened to use a variable named C<$OUT>, you will have to change them
  1660. to use some other variable or all sorts of strangeness will result.
  1661.  
  1662. Between versions 0.1b and 1.00 the behavior of the \ metacharacter
  1663. changed.  In 0.1b, \\ was special everywhere, and the template
  1664. processor always replaced it with a single backslash before passing
  1665. the code to Perl for evaluation.  The rule now is more complicated but
  1666. probably more convenient.  See the section on backslash processing,
  1667. below, for a full discussion.
  1668.  
  1669. =head2 Backslash Processing
  1670.  
  1671. In C<Text::Template> beta versions, the backslash was special whenever
  1672. it appeared before a brace or another backslash.  That meant that
  1673. while C<{"\n"}> did indeed generate a newline, C<{"\\"}> did not
  1674. generate a backslash, because the code passed to Perl for evaluation
  1675. was C<"\"> which is a syntax error.  If you wanted a backslash, you
  1676. would have had to write C<{"\\\\"}>.
  1677.  
  1678. In C<Text::Template> versions 1.00 through 1.10, there was a bug:
  1679. Backslash was special everywhere.  In these versions, C<{"\n"}>
  1680. generated the letter C<n>.
  1681.  
  1682. The bug has been corrected in version 1.11, but I did not go back to
  1683. exactly the old rule, because I did not like the idea of having to
  1684. write C<{"\\\\"}> to get one backslash.  The rule is now more
  1685. complicated to remember, but probably easier to use.  The rule is now:
  1686. Backslashes are always passed to Perl unchanged I<unless> they occur
  1687. as part of a sequence like C<\\\\\\{> or C<\\\\\\}>.  In these
  1688. contexts, they are special; C<\\> is replaced with C<\>, and C<\{> and
  1689. C<\}> signal a literal brace. 
  1690.  
  1691. Examples:
  1692.  
  1693.     \{ foo \}
  1694.  
  1695. is I<not> evaluated, because the C<\> before the braces signals that
  1696. they should be taken literally.  The result in the output looks like this: 
  1697.  
  1698.     { foo }
  1699.  
  1700.  
  1701. This is a syntax error:
  1702.  
  1703.     { "foo}" }
  1704.  
  1705. because C<Text::Template> thinks that the code ends at the first C<}>,
  1706. and then gets upset when it sees the second one.  To make this work
  1707. correctly, use
  1708.  
  1709.     { "foo\}" }
  1710.  
  1711. This passes C<"foo}"> to Perl for evaluation.  Note there's no C<\> in
  1712. the evaluated code.  If you really want a C<\> in the evaluated code,
  1713. use
  1714.  
  1715.     { "foo\\\}" }
  1716.  
  1717. This passes C<"foo\}"> to Perl for evaluation.
  1718.  
  1719. Starting with C<Text::Template> version 1.20, backslash processing is
  1720. disabled if you use the C<DELIMITERS> option to specify alternative
  1721. delimiter strings.
  1722.  
  1723. =head2 A short note about C<$Text::Template::ERROR>
  1724.  
  1725. In the past some people have fretted about `violating the package
  1726. boundary' by examining a variable inside the C<Text::Template>
  1727. package.  Don't feel this way.  C<$Text::Template::ERROR> is part of
  1728. the published, official interface to this package.  It is perfectly OK
  1729. to inspect this variable.  The interface is not going to change.
  1730.  
  1731. If it really, really bothers you, you can import a function called
  1732. C<TTerror> that returns the current value of the C<$ERROR> variable.
  1733. So you can say:
  1734.  
  1735.     use Text::Template 'TTerror';
  1736.  
  1737.     my $template = new Text::Template (SOURCE => $filename);
  1738.     unless ($template) {
  1739.       my $err = TTerror;
  1740.       die "Couldn't make template: $err; aborting";
  1741.     }
  1742.  
  1743. I don't see what benefit this has over just doing this:
  1744.  
  1745.     use Text::Template;
  1746.  
  1747.     my $template = new Text::Template (SOURCE => $filename)
  1748.       or die "Couldn't make template: $Text::Template::ERROR; aborting";
  1749.  
  1750. But if it makes you happy to do it that way, go ahead.
  1751.  
  1752. =head2 Sticky Widgets in Template Files
  1753.  
  1754. The C<CGI> module provides functions for `sticky widgets', which are
  1755. form input controls that retain their values from one page to the
  1756. next.   Sometimes people want to know how to include these widgets
  1757. into their template output.
  1758.  
  1759. It's totally straightforward.  Just call the C<CGI> functions from
  1760. inside the template:
  1761.  
  1762.     { $q->checkbox_group(NAME => 'toppings',
  1763.                    LINEBREAK => true,
  1764.                  COLUMNS => 3,
  1765.                  VALUES => \@toppings,
  1766.                 );
  1767.     }
  1768.  
  1769. =head2 Automatic preprocessing of program fragments
  1770.  
  1771. It may be useful to preprocess the program fragments before they are
  1772. evaluated.  See C<Text::Template::Preprocess> for more details.
  1773.  
  1774. =head2 Author
  1775.  
  1776. Mark-Jason Dominus, Plover Systems
  1777.  
  1778. Please send questions and other remarks about this software to
  1779. C<mjd-perl-template+@plover.com>
  1780.  
  1781. You can join a very low-volume (E<lt>10 messages per year) mailing
  1782. list for announcements about this package.  Send an empty note to
  1783. C<mjd-perl-template-request@plover.com> to join.
  1784.  
  1785. For updates, visit C<http://www.plover.com/~mjd/perl/Template/>.
  1786.  
  1787. =head2 Support?
  1788.  
  1789. This software is version 1.43.  It may have bugs.  Suggestions and bug
  1790. reports are always welcome.  Send them to
  1791. C<mjd-perl-template+@plover.com>.  (That is my address, not the address
  1792. of the mailing list.  The mailing list address is a secret.)
  1793.  
  1794. =head1 LICENSE
  1795.  
  1796.     Text::Template version 1.43
  1797.     Copyright (C) 2001 Mark Jason Dominus
  1798.  
  1799.     This program is free software; you can redistribute it and/or
  1800.     modify it under the terms of the GNU General Public License as
  1801.     published by the Free Software Foundation; either version 2 of the
  1802.     License, or (at your option) any later version.  You may also can
  1803.     redistribute it and/or modify it under the terms of the Perl
  1804.     Artistic License.
  1805.  
  1806.     This program is distributed in the hope that it will be useful,
  1807.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  1808.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  1809.     GNU General Public License for more details.
  1810.  
  1811.     You should have received copies of the GNU General Public License
  1812.     along with this program; if not, write to the Free Software
  1813.     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  1814.  
  1815.  
  1816. =head1 THANKS
  1817.  
  1818. Many thanks to the following people for offering support,
  1819. encouragement, advice, bug reports, and all the other good stuff.  
  1820.  
  1821. David H. Adler /
  1822. Joel Appelbaum /
  1823. Klaus Arnhold /
  1824. Kevin Atteson /
  1825. Chris.Brezil /
  1826. Mike Brodhead /
  1827. Tom Brown /
  1828. Tim Bunce /
  1829. Juan E. Camacho /
  1830. Itamar Almeida de Carvalho /
  1831. Joseph Cheek /
  1832. Gene Damon /
  1833. San Deng /
  1834. Bob Dougherty /
  1835. Marek Grac /
  1836. Dan Franklin /
  1837. gary at dls.net /
  1838. Todd A. Green /
  1839. Donald L. Greer Jr. /
  1840. Michelangelo Grigni /
  1841. Tom Henry /
  1842. Matt X. Hunter /
  1843. Robert M. Ioffe /
  1844. Daniel LaLiberte /
  1845. Reuven M. Lerner /
  1846. Trip Lilley / 
  1847. Yannis Livassof /
  1848. David Marshall /
  1849. Joel Meulenberg /
  1850. Jason Moore /
  1851. Chris Nandor /
  1852. Bek Oberin /
  1853. Steve Palincsar /
  1854. Ron Pero /
  1855. Hans Persson /
  1856. Jonathan Roy /
  1857. Shabbir J. Safdar /
  1858. Jennifer D. St Clair /
  1859. Uwe Schneider /
  1860. Randal L. Schwartz /
  1861. Michael G Schwern /
  1862. Brian C. Shensky /
  1863. Niklas Skoglund /
  1864. Tom Snee /
  1865. Hans Stoop /
  1866. Michael J. Suzio /
  1867. Dennis Taylor /
  1868. James H. Thompson /
  1869. Shad Todd /
  1870. Lorenzo Valdettaro /
  1871. Larry Virden /
  1872. Andy Wardley /
  1873. Archie Warnock /
  1874. Matt Womer /
  1875. Andrew G Wood /
  1876. Daini Xie /
  1877. Michaely Yeung
  1878.  
  1879. Special thanks to:
  1880.  
  1881. =over 2
  1882.  
  1883. =item Jonathan Roy 
  1884.  
  1885. for telling me how to do the C<Safe> support (I spent two years
  1886. worrying about it, and then Jonathan pointed out that it was trivial.)
  1887.  
  1888. =item Ranjit Bhatnagar 
  1889.  
  1890. for demanding less verbose fragments like they have in ASP, for
  1891. helping me figure out the Right Thing, and, especially, for talking me
  1892. out of adding any new syntax.  These discussions resulted in the
  1893. C<$OUT> feature.
  1894.  
  1895. =back
  1896.  
  1897. =head2 Bugs and Caveats
  1898.  
  1899. C<my> variables in C<fill_in> are still susceptible to being clobbered
  1900. by template evaluation.  They all begin with C<fi_>, so avoid those
  1901. names in your templates.
  1902.  
  1903. The line number information will be wrong if the template's lines are
  1904. not terminated by C<"\n">.  You should let me know if this is a
  1905. problem.  If you do, I will fix it.
  1906.  
  1907. The C<$OUT> variable has a special meaning in templates, so you cannot
  1908. use it as if it were a regular variable.
  1909.  
  1910. There are not quite enough tests in the test suite.
  1911.  
  1912. =cut
  1913.